#pragma once
#include<stdio.h>
#include<string>
#include<set>

#define BUFSIZE 1024
class command
{
public:
    command()
    {
        white_list.insert("ls");
        white_list.insert("ls -l");
        white_list.insert("ll");
        white_list.insert("pwd");
        white_list.insert("who");
        white_list.insert("who am i");
    }
private:
    bool cheat_cmd_permit(std::string& cmd)
    {
        auto pos=white_list.find(cmd);
        if(pos==white_list.end())
        {
            return false;
        }
        return true;
    }
public:
    std::string excute(std::string& commandstr)
    {
        if(!cheat_cmd_permit(commandstr))
        {
            return "permission denied";
        }
        //将commandstr的命令进行执行
        FILE* exe_result=popen(commandstr.c_str(),"r");
        if(exe_result==nullptr)
        {
            return "execute fail";
        }
        //执行结果全都保存在exe_result当中
        std::string ret;
        char inbuffer[BUFSIZE];
        while(true)
        {
            if(fgets(inbuffer,sizeof(inbuffer),exe_result)==nullptr)
            {
                break;
            }
            ret+=inbuffer;
        }
        return ret;
    }
private:
    std::set<std::string> white_list;//白名单，能让客户端执行并返回的命令
};